package cn.wolfcode.web.controller;

import cn.wolfcode.ssm.domain.Department;
import cn.wolfcode.ssm.page.PageResult;
import cn.wolfcode.ssm.query.QueryObject;
import cn.wolfcode.ssm.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/department")

public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") QueryObject qo){
        PageResult<Department> pageResult = departmentService.query(qo);
        model.addAttribute("pageResult",pageResult);
        System.out.println("第一滴血,我实乃万事之先");
        return "/department/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        if (id != null){
            departmentService.delete(id);
        }
        return "redirect:/department/list.do";
    }

    @RequestMapping("/input")
    public String input(Model model, Long id){
        if (id != null){
            Department department = departmentService.get(id);
            model.addAttribute("department",department);
        }
        return "/department/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department, Long id){
        if (id != null){
            departmentService.update(department);
        }else{
            departmentService.save(department);
        }
        return "redirect:/department/list.do";
    }
}
